package com.woniuxy.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.QuestionTopic;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.Test;
import com.woniuxy.entity.User;
import com.woniuxy.service.TestService;
import com.woniuxy.test;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("test")

public class testController {
    @Autowired
    TestService testService;

    @RequestMapping("tList")
    public List<Test> findTestList(Map map) {
        return testService.findAllTest(map);


    }

    @RequestMapping("/findallTest/{currentPage}/{pageSize}")
    public ResponseEntity findAllQ(@PathVariable int currentPage, @PathVariable int pageSize, @RequestBody String jsonString) {
        Map<String, Object> map = new HashMap<>();
        //将json字符串转换为json对象
        JSONObject json = JSONObject.parseObject(jsonString);
        //将json对象循环转换为map
        Iterator it = json.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it.next();
            if (entry.getValue().toString().length() == 0 || entry.getValue().toString().equals("")) {
                map.put(entry.getKey(), null);
            } else {
                map.put(entry.getKey(), entry.getValue());
            }
        }
//        System.out.println("打印map" + map);
        //System.out.println(currentPage+"分隔符"+pageSize);
        PageHelper.startPage(currentPage, pageSize);
        List<Test> all = testService.findAllTest(map);
        //添加分页信息
        PageInfo<Test> testPageInfo = new PageInfo<>(all);
        return new ResponseEntity(testPageInfo);
    }

    @RequestMapping("add")
    public ResponseEntity addTest(@RequestBody String json) {
        return testService.addTest(json);
    }

    @RequestMapping("del/{id}")
    public ResponseEntity del(@PathVariable Integer id) {
        return testService.delTest(id);
    }

    @RequestMapping("update")
    @RequiresPermissions("system")
    public ResponseEntity update(@RequestBody String json) {
        return testService.update(json);
    }

    @RequestMapping("findOnesTest")
    public ResponseEntity findOnesTest(@RequestBody Map map) {
        return new ResponseEntity(testService.findOnesTest(map));
    }

    @RequestMapping("findAllOnesTest/{currentPage}/{pageSize}")
    public ResponseEntity findAllOnesTest(@PathVariable int currentPage, @PathVariable int pageSize,@RequestBody Map map) {

        System.out.println(currentPage+"分隔符"+pageSize);
        PageHelper.startPage(currentPage, pageSize);
        List<Test> all = testService.findAllOnesTest(map);
        //添加分页信息
        PageInfo<Test> testPageInfo = new PageInfo<>(all);
        return new ResponseEntity(testPageInfo);


    }
}
